<?php

use console\db\Migration;

/**
 * 创建资讯相关信息表
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m190102_180239_t_cms extends Migration {

    /**
     * {@inheritdoc}
     */
    public function up() {

        /**
         * 频道表
         * @table `cms_channel`
         */
        $this->tableBuilder([
            'name' => 'cms_channel',
            'comment' => '频道表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->string(50)->notNull()->comment('名称'),
                'abbr' => $this->string(50)->notNull()->comment('缩写'),
                'icon' => $this->char(255)->defaultValue('')->comment('图标'),
                'cover' => $this->char(255)->defaultValue('')->comment('封面'),
                'template' => $this->char(20)->defaultValue('')->comment('自定义显示模版'),
                'link' => $this->char(255)->defaultValue('')->comment('跳转链接'),
                'listorder' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('排序'),
                'recommend_at' => $this->unixTimestamp()->comment('推荐时间'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(0)->comment('状态'),
                'created_at' => $this->unixTimestamp(),
                'updated_at' => $this->unixTimestamp(),
                'deleted_at' => $this->unixTimestamp(),
            ]
        ]);

        /**
         * 标签表
         * @todo create table `%cms_tag`.
         */
        $this->tableBuilder([
            'name' => 'cms_tag',
            'comment' => '标签表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->string(50)->notNull()->comment('标签名称'),
                'admin_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('创建人ID'),
                'created_at' => $this->unixTimestamp(),
                'updated_at' => $this->unixTimestamp(),
                'deleted_at' => $this->unixTimestamp(),
                'status' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(10)->comment('状态'),
            ]
        ]);

        /**
         * 文章表
         * @todo create table `%cms_article`.
         */
        $this->tableBuilder([
            'name' => 'cms_article',
            'comment' => '文章表',
            'columns' => [
                'id' => $this->primaryKey(),
                'channel_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('频道ID'),
                'title' => $this->string(80)->notNull()->comment('标题'),
                'format' => $this->enum(['default', 'gallery', 'video'])->notNull()->defaultValue('default')->comment('文章格式：default普通文章，gallery图集，video视频'),
                'thumb' => $this->char(255)->defaultValue('')->comment('封面图'),
                'keywords' => $this->string(255)->defaultValue('')->comment('关键词'),
                'description' => $this->string(255)->defaultValue('')->comment('导读简介'),
                'url' => $this->char(255)->defaultValue('')->comment('外链地址'),
                'duration' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('视频时长'),
                'picture_nums' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('组图数量'),
                'allow_comment' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(1)->comment('允许评论，0禁止，1开启'),
                'author' => $this->string(20)->defaultValue('')->comment('撰稿人'),
                'author_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('撰稿人ID'),
                'recommend_id' => $this->unixTimestamp()->comment('推荐ID'),
                'created_at' => $this->unixTimestamp(),
                'updated_at' => $this->unixTimestamp(),
                'deleted_at' => $this->unixTimestamp(),
                'status' => $this->tinyint(1)->unsigned()->notNull()->defaultValue(10)->comment('状态'),
            ],
            'index' => [
                [
                    'columns' => 'channel_id',
                ],
            ]
        ]);

        /**
         * 文章详情表
         * @todo create table `%cms_article_data`.
         */
        $this->tableBuilder([
            'name' => 'cms_article_data',
            'comment' => '文章详情表',
            'columns' => [
                'id' => $this->primaryKey(),
                'article_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('文章ID'),
                'body' => $this->text()->comment('文章图文详情'),
            ],
            'index' => [
                [
                    'columns' => 'article_id',
                ],
            ]
        ]);

        /**
         * 文章视频表
         * @todo create table `%cms_article_video`.
         */
        $this->tableBuilder([
            'name' => 'cms_article_video',
            'comment' => '文章视频表',
            'columns' => [
                'id' => $this->primaryKey(),
                'article_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('文章ID'),
                'path' => $this->char(255)->defaultValue('')->comment('视频地址'),
                'duration' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('视频时长'),
                'size' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('视频大小'),
                'le_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('乐视视频ID'),
                'le_unique' => $this->char(32)->defaultValue('')->comment('乐视视频标识码'),
            ],
            'index' => [
                [
                    'columns' => 'article_id',
                ],
            ]
        ]);

        /**
         * 文章图集表
         * @todo create table `%cms_article_gallery`.
         */
        $this->tableBuilder([
            'name' => 'cms_article_gallery',
            'comment' => '文章图集表',
            'columns' => [
                'id' => $this->primaryKey(),
                'article_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('文章ID'),
                'original' => $this->char(255)->notNull()->defaultValue('')->comment('原图地址'),
                'width' => $this->smallInteger(4)->unsigned()->notNull()->defaultValue(0)->comment('图片宽度'),
                'height' => $this->smallInteger(4)->unsigned()->notNull()->defaultValue(0)->comment('图片高度'),
                'description' => $this->string(255)->defaultValue('')->comment('图片描述'),
                'listorder' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('显示排序'),
            ],
            'index' => [
                [
                    'columns' => 'article_id',
                ],
            ]
        ]);


        /**
         * 文章推荐频道表
         * @table `cms_article_channel`.
         */
        $this->tableBuilder([
            'name' => 'cms_article_channel',
            'comment' => '文章推荐频道表',
            'columns' => [
                'id' => $this->primaryKey(),
                'article_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('文章ID'),
                'channel_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('频道ID'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'channel_id',
                ],
                [
                    'columns' => 'article_id',
                ],
            ]
        ]);

        /**
         * 文章标签关联表
         * @table `cms_article_tag`.
         */
        $this->tableBuilder([
            'name' => 'cms_article_tag',
            'comment' => '文章标签关联表',
            'columns' => [
                'id' => $this->primaryKey(),
                'article_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('文章ID'),
                'tag_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('标签ID'),
            ],
            'index' => [
                [
                    'columns' => 'article_id',
                ],
                [
                    'columns' => 'tag_id',
                ],
            ]
        ]);

        /**
         * 文章评论表
         * @todo create table `%cms_article_comment`.
         */
        $this->tableBuilder([
            'name' => 'cms_article_comment',
            'comment' => '文章评论表',
            'columns' => [
                'id' => $this->primaryKey(),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'article_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('文章ID'),
                'comment_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('评论ID'),
                'reply_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('回复ID'),
                'post' => $this->string(1000)->notNull()->comment('评论内容'),
                'attach' => $this->text()->comment('评论附件'),
                'author' => $this->string(30)->defaultValue('')->comment('作者'),
                'listener' => $this->string(30)->defaultValue('')->comment('被回复人'),
                'likes' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('点赞数'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('评论状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ],
            'index' => [
                [
                    'columns' => 'user_id',
                ],
                [
                    'columns' => 'article_id',
                ],
                [
                    'columns' => 'comment_id',
                ],
            ]
        ]);

        /**
         * 文章数据表
         * @table `cms_article_stats`.
         */
        $this->tableBuilder([
            'name' => 'cms_article_stats',
            'comment' => '文章数据表',
            'columns' => [
                'id' => $this->primaryKey(),
                'article_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('文章ID'),
                'views' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('浏览次数'),
                'likes' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('点赞数'),
                'comments' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('评论数'),
                'created_at' => $this->unixTimestamp(),
                'updated_at' => $this->unixTimestamp(),
            ],
            'index' => [
                [
                    'columns' => 'article_id',
                ],
            ]
        ]);

        /**
         * 文章评论点赞表
         * @todo create table `%cms_article_like`.
         */
        $this->tableBuilder([
            'name' => 'cms_article_like',
            'comment' => '文章评论点赞表',
            'columns' => [
                'id' => $this->primaryKey(),
                'article_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('文章ID'),
                'comment_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('评论ID'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'article_id',
                ],
                [
                    'columns' => 'comment_id',
                ],
                [
                    'columns' => 'user_id',
                ],
            ]
        ]);

        /**
         * 文章屏蔽表
         * @table `cms_article_shield`
         */
        $this->tableBuilder([
            'name' => 'cms_article_shield',
            'comment' => '文章屏蔽表',
            'columns' => [
                'id' => $this->primaryKey(),
                'article_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('文章ID'),
                'tag_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('标签ID'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('用户ID'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'article_id',
                ],
                [
                    'columns' => 'tag_id',
                ],
                [
                    'columns' => 'user_id',
                ],
            ]
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function down() {
        $this->dropTable('{{%cms_article_shield}}');
        $this->dropTable('{{%cms_article_like}}');
        $this->dropTable('{{%cms_article_stats}}');
        $this->dropTable('{{%cms_article_comment}}');
        $this->dropTable('{{%cms_article_channel}}');
        $this->dropTable('{{%cms_article_tag}}');
        $this->dropTable('{{%cms_article_gallery}}');
        $this->dropTable('{{%cms_article_video}}');
        $this->dropTable('{{%cms_article_data}}');
        $this->dropTable('{{%cms_article}}');
        $this->dropTable('{{%cms_tag}}');
        $this->dropTable('{{%cms_channel}}');
    }

}
